package cn.iocoder.devlop.module.annual.report.wssubcapital.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.NumberUtil;
import cn.iocoder.devlop.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.devlop.module.annual.report.baseinfo.service.BaseinfoService;
import cn.iocoder.devlop.module.annual.report.utils.IdGenerateUtil;
import cn.iocoder.devlop.module.annual.report.utils.ReportYearHelper;
import cn.iocoder.devlop.module.annual.report.wssubcapital.controller.vo.WsSubcapitalCreateReqVO;
import cn.iocoder.devlop.module.annual.report.wssubcapital.controller.vo.WsSubcapitalOverwriteVO;
import cn.iocoder.devlop.module.annual.report.wssubcapital.convert.WsSubcapitalConvert;
import cn.iocoder.devlop.module.annual.report.wssubcapital.dal.WsSubcapitalMapper;
import cn.iocoder.devlop.module.annual.report.wssubcapital.dal.dataobject.WsSubcapitalDO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

/**
 * 外商年报投资者信息 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class WsSubcapitalServiceImpl implements WsSubcapitalService {

    @Resource
    private WsSubcapitalMapper wsSubcapitalMapper;

    @Resource
    private BaseinfoService baseinfoService;


    @Override
    public List<WsSubcapitalDO> getWsSubcapitalList(String regno, String ancheyear) {
        ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
        String ancheid = IdGenerateUtil.getAncheid(regno, ancheyear);
        return wsSubcapitalMapper.selectListByAncheid(ancheid);
    }

    @Override
    public void deleteWsSubcapitalAll(String regno, String ancheyear) {
        String ancheid = IdGenerateUtil.getAncheid(regno, ancheyear);
        ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
        LambdaQueryWrapperX<WsSubcapitalDO> wrapperX = new LambdaQueryWrapperX<>();
        wrapperX.eq(WsSubcapitalDO::getAncheid, ancheid);
        wsSubcapitalMapper.delete(wrapperX);
    }

    @Override
    public void createWsSubcapitalByList(String regno, String ancheyear, List<WsSubcapitalCreateReqVO> wsSubcapitalList) {
        String ancheid = IdGenerateUtil.getAncheid(regno, ancheyear);
        ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
        if (CollUtil.isNotEmpty(wsSubcapitalList)) {
            List<WsSubcapitalDO> wsSubcapitalDOS = WsSubcapitalConvert.INSTANCE.convertList01(wsSubcapitalList);
            for (WsSubcapitalDO wsSubcapital : wsSubcapitalDOS) {
                wsSubcapital.setAncheid(ancheid);
                wsSubcapital.setInvid(IdUtil.simpleUUID());
            }
            wsSubcapitalMapper.insertBatch(wsSubcapitalDOS);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void overwriteWsSubcapital(WsSubcapitalOverwriteVO updateReqVO) {
        deleteWsSubcapitalAll(updateReqVO.getRegno(), updateReqVO.getAncheyear());
        createWsSubcapitalByList(updateReqVO.getRegno(), updateReqVO.getAncheyear(), updateReqVO.getWsSubcapitalList());
        baseinfoService.updateState(updateReqVO.getRegno(), updateReqVO.getAncheyear());
    }

}
